package com.wenkang.stu.service;

import com.baomidou.mybatisplus.core.conditions.query.QueryWrapper;
import com.baomidou.mybatisplus.extension.service.IService;
import com.baomidou.mybatisplus.extension.service.impl.ServiceImpl;
import com.wenkang.stu.mapper.RoleMenuMapper;
import com.wenkang.stu.mapper.UserRoleMapper;
import com.wenkang.stu.pojo.SysRole;
import com.wenkang.stu.pojo.SysRoleMenu;
import com.wenkang.stu.pojo.SysUserRole;
import org.springframework.beans.factory.annotation.Autowired;
import org.springframework.stereotype.Service;
import org.springframework.transaction.annotation.Transactional;
import org.springframework.util.CollectionUtils;

@Service
public class SysUserRoleService extends ServiceImpl<UserRoleMapper, SysUserRole> implements IService<SysUserRole> {
    @Autowired
    private RoleMenuMapper roleMenuMapper;

    /**
     * 更新角色权限
     *
     * @param role
     */
    @Transactional
    public void updateRoleMenu(SysRole role) {
        QueryWrapper<SysRoleMenu> queryWrapper = new QueryWrapper<>();
        roleMenuMapper.delete(queryWrapper.eq("role_id", role.getId()));
        if (CollectionUtils.isEmpty(role.getMenuIds())) {
            return;
        }
        roleMenuMapper.insert(role.getId(), role.getMenuIds());
    }
}
